package cn.edu.scau.cmi.ema.controller;

import cn.edu.scau.cmi.ema.base.CmiPagination;
import cn.edu.scau.cmi.ema.base.CmiEmaController;
import cn.edu.scau.cmi.ema.domain.Law;
import cn.edu.scau.cmi.ema.util.CmiHibernateProxyUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Set;

@Controller("LawController")
public class LawController extends CmiEmaController {

	@RequestMapping("/listLaws")
	public ModelAndView listLaws(HttpServletRequest request, @ModelAttribute CmiPagination pagination) {
		ModelAndView mav = new ModelAndView();

		pagination = lawPaginationService.updatePagination(request, pagination);
		Set<Law> laws = lawPaginationService.getPaginationLaws(pagination);

		mav.addObject("laws", laws);
		mav.addObject("entityName", "law");
		mav.addObject("pagination", pagination);
		mav.setViewName("law/listLaws");
		return mav;
	}

	public String listLaws() {
		return "redirect:/listLaws";
	}

	@RequestMapping("/viewLaw")
	public ModelAndView viewLaw(@RequestParam Integer lawId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("law", lawDAO.findLawByPrimaryKey(lawId));
		mav.setViewName("law/viewLaw");
		return mav;
	}

	@RequestMapping(value = "/confirmDeleteLaws")
	public ModelAndView confirmDeleteLaws(
			@RequestParam(value = "deleteLawsIds", required = true) Set<Integer> deleteLawsIds) {
		ModelAndView mav = new ModelAndView();

		Set<Law> deleteLaws = lawDAO.findLawsByIds(deleteLawsIds);
		Set<Law> referencedLaws = lawService.loadReferenceLaws(deleteLaws);
		boolean hasReferenced;
		if (referencedLaws.size() == 0) {
			mav.addObject("deleteEntitys", deleteLaws);
			hasReferenced = false;
		} else {
			deleteLaws.removeAll(referencedLaws);
			mav.addObject("referencedEntitys", referencedLaws);
			mav.addObject("deleteEntitys", deleteLaws);
			hasReferenced = true;
		}
		mav.addObject("entityName", "Law");
		mav.addObject("hasReferenced", hasReferenced);
		mav.setViewName("forward:/listLaws");
		return mav;
	}

	@RequestMapping("/deleteLaw")
	public String deleteLaw(@RequestParam Integer lawId) {
		Law law = lawDAO.findLawByPrimaryKey(lawId);
		lawService.deleteLaw(law);
		return "forward:/listLaws";
	}

	@RequestMapping("/confirmDeleteLaw")
	public ModelAndView confirmDeleteLaw(@RequestParam Integer lawId) {

		ModelAndView mav = new ModelAndView();

		mav.addObject("law", lawDAO.findLawByPrimaryKey(lawId));

		mav.setViewName("law/deleteLaw");

		return mav;
	}

	@RequestMapping("/deleteLaws")
	public String deleteLaws(@RequestParam(value = "deleteLawsId", required = true) Set<Integer> deleteLawsId) {
		lawService.deleteLaws(deleteLawsId);
		return "forward:/listLaws";
	}

	@RequestMapping("/editLaw")
	public ModelAndView editLaw(@RequestParam Integer lawId) {

		ModelAndView mav = new ModelAndView();

		Law law = lawDAO.findLawByPrimaryKey(lawId);
		// 将需要编辑的对象加到mav对象中
		mav.addObject("law", law);

		// 把所有的引用law对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(lawService.loadLawObjectReferencedSet(law));

		// 将编辑页面law添加到mav中
		mav.setViewName("law/editLaw");
		return mav;
	}

	@RequestMapping("/newLaw")
	public ModelAndView newLaw() {
		ModelAndView mav = new ModelAndView();
		Law law = new Law();

		mav.addObject("law", law);

		// 把所有的引用law对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(lawService.loadLawObjectReferencedSet(law));

		mav.addObject("newFlag", true);
		mav.setViewName("law/editLaw");

		return mav;
	}

	@RequestMapping("/saveLaw")
	public ModelAndView saveLaw(@ModelAttribute Law law) {

		ModelAndView mav = new ModelAndView();

		Law existingLaw = CmiHibernateProxyUtil.deproxy(lawDAO.findLawByPhysicalPrimaryKey(law.getName()));

		// 新建记录
		if (law.getId() == null) {
			if (law.equals(existingLaw)) {

				mav.addAllObjects(lawService.loadLawClassReferencedSet(law));
				// mav = editContinue(mav, law);
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("law/editLaw");
			} else {
				lawService.saveLaw(law);
				mav.setViewName("forward:/listLaws");
			}
		}
		// 编辑记录
		else {
			Law originLaw = CmiHibernateProxyUtil.deproxy(lawDAO.findLawByPrimaryKey(law.getId()));
			if (!law.equals(originLaw) && law.equals(existingLaw)) {
				mav.addAllObjects(lawService.loadLawClassReferencedSet(law));
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("law/editLaw");
			} else {
				lawService.saveLaw(law);
				mav.setViewName("forward:/listLaws");
			}
		}
		return mav;
	}

	@RequestMapping("/lawController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request,
			@ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

}
